package com.landeb.shop.huo.act;

import android.content.Context;
import android.content.Intent;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.Toast;

import com.landeb.shop.R;
import com.landeb.shop.huo.MyApplication;
import com.landeb.shop.huo.adapter.ActShopListLvAdapter;
import com.landeb.shop.huo.bean.BeanActShopList;
import com.landeb.shop.huo.bean.BeanAliPayRequst;
import com.landeb.shop.huo.httpRequest.ActShopListRequset;
import com.landeb.shop.huo.httpRequest.UserConfirmationRequest;
import com.landeb.shop.huo.view.ShopListHeadView;

import java.util.ArrayList;
import java.util.List;

import okhttp3.OkHttpClient;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava.RxJavaCallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;
import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

public class ShopListActivity extends BaseActivity implements View.OnClickListener {

    private String mShopId;

    private LinearLayout btnBack;
    private ListView mListView;
    private SwipeRefreshLayout mSRL;

    private List<BeanActShopList.DataBean> mList;
    private ActShopListLvAdapter mAdapter;

    private String mUrl;
    private Retrofit mRetrofit;
    private ActShopListRequset mRequest;
    private boolean isRefreshing = false;

    public static void toAct(Context context) {
        Intent intent = new Intent(context, ShopListActivity.class);
        context.startActivity(intent);
    }

    public static void toAct(Context context, String shopId) {
        Intent intent = new Intent(context, ShopListActivity.class);
        intent.putExtra("shopId", shopId);
        context.startActivity(intent);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_shop_list);

        Intent intent = getIntent();
        mShopId = intent.getStringExtra("shopId");

        OkHttpClient client = MyApplication.getClient();
        mUrl = MyApplication.BASE_URL;
        mRetrofit = new Retrofit.Builder()
                .baseUrl(mUrl)
                .addConverterFactory(GsonConverterFactory.create())
                .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
                .client(client)
                .build();
        mRequest = mRetrofit.create(ActShopListRequset.class);


        initView();
        initData();
        initEvent();

        refresh();
    }

    private void initEvent() {

        btnBack.setOnClickListener(this);

        mSRL.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                refresh();
            }
        });



        mAdapter = new ActShopListLvAdapter(this, mList);

        ShopListHeadView headView=new ShopListHeadView(this);
        headView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // TODO: 2016/12/8 查看全部宝贝 
//                Toast.makeText(ShopListActivity.this, "HEADVIEW", Toast.LENGTH_SHORT).show();
                ShopSearchResultActivity.toAct(ShopListActivity.this,mShopId,null);
            }
        });
        
        mListView.addHeaderView(headView,null,false);

        mListView.setAdapter(mAdapter);


    }

    private void refresh() {

        mSRL.setRefreshing(true);

        if (!isRefreshing) {

            isRefreshing = true;

            mRequest.getShopList(mShopId)
                    .subscribeOn(Schedulers.io())
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe(new Subscriber<BeanActShopList>() {
                        @Override
                        public void onCompleted() {
                            Log.e("getShopList", "onCompleted: ");
                            mSRL.setRefreshing(false);
                            isRefreshing = false;
                        }

                        @Override
                        public void onError(Throwable e) {
                            Log.e("getShopList", "onError: " + e.toString());
                            mSRL.setRefreshing(false);
                            isRefreshing = false;
                        }

                        @Override
                        public void onNext(BeanActShopList beanActShopList) {

                            if (beanActShopList.getStatus() == 1) {

                                mList.clear();
                                mList.addAll(beanActShopList.getData());
                                mAdapter.notifyDataSetChanged();

                            } else {
                                Toast.makeText(ShopListActivity.this, beanActShopList.getInfo(), Toast.LENGTH_SHORT).show();
                            }

                        }
                    });


        }

    }

    private void initData() {

        mList = new ArrayList<>();

    }

    private void initView() {
        btnBack = (LinearLayout) findViewById(R.id.act_shopList_btn_back);
        mSRL = (SwipeRefreshLayout) findViewById(R.id.act_shopList_srl);
        mSRL.setColorSchemeColors(getResources().getColor(R.color.colorPrimary));

        mListView = (ListView) findViewById(R.id.act_shopList_lv);
    }

    @Override
    public void onClick(View v) {

        switch (v.getId()) {
            case R.id.act_shopList_btn_back:
                // TODO: 2016/12/8 返回
                onBackPressed();
                break;

        }

    }
}
